package com.tanhua.controller;

import com.tanhua.exeception.BusinessException;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 杰哥不要啦~
 * @date 2021/10/22
 */
@RestController
@RequestMapping("/user")
public class LoginController{
    @Autowired
    private UserServer userServer;
    /**
     * 获取登录验证码
     *  请求参数phone（map）
     *  void
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userServer.sendMsg(phone);
        ///ResponseEntity.status(500).body("出错了");
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
        try{
            //1、调用map集合获取请求参数
            String phone = (String) map.get("phone");
            String verificationCode = (String) map.get("verificationCode");
            //2、调用userService完成用户登录
            Map retMap  = userServer.loginVerification(phone,verificationCode);
            //3、构造返回
            return  ResponseEntity.ok(retMap);
        }catch (BusinessException be){
            ErrorResult errorResult = be.getErrorResult();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
        }
    }
}
